/**
 * 
 */
package com.dingjian.meal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingjian.base.CoreEntity;
import com.dingjian.base.common.Pagination;
import com.dingjian.base.service.BaseService;
import com.dingjian.framework.base.controller.ListController;
import com.dingjian.meal.model.MealRecord;
import com.dingjian.meal.model.MealTime;
import com.dingjian.meal.service.MealRecordService;

/**
 *
 * @author FengMy
 * @since 2012-6-8
 */
@Controller
@RequestMapping(value="meal/record/*")
public class MealRecordListController extends ListController {
	@Resource(name="mealRecordService")
	private MealRecordService mealRecordService;
	
	public String list(){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("enable", 1);
		List<MealTime> times = queryExecutor.execQuery("com.dingjian.meal.dao.MealTimeDao.select", new Pagination<MealTime>(10, 1), param).getItems();
		if(times.size()>0){
			getRequest().setAttribute("time", times.get(0));
		}
		return super.list();
	}
	
	@RequestMapping(value="summary")
	public String summary(ModelMap model){
		String time = getString("time");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("time", time);
		List<Map<String, Object>> result = mealRecordService.summary(param );
		param.put("items", result);
		model.put("page", JSONObject.fromObject(param, getDefaultJsonConfig()).toString());
		return "meal/recoreSummary";
	}
	
	@RequestMapping(value="exists")
	public void exists(HttpServletResponse response){
		String person = getString("person");
		String time = getString("time");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("person", person);
		param.put("time", time);
		boolean b = mealRecordService.exists(param);
		param.put("exists", b);
		outPrint(response, JSONObject.fromObject(param, getDefaultJsonConfig()));
	}

	@Override
	protected CoreEntity createNewEntity() {
		MealRecord record = new MealRecord();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("enable", 1);
		
		List<MealTime> times = queryExecutor.execQuery("com.dingjian.meal.dao.MealTimeDao.select", new Pagination<MealTime>(10, 1), param).getItems();
		if(times.size()>0){
			record.setMealTime(times.get(0));
		}
		return record;
	}

	@Override
	protected String getListView() {
		return "meal/mealRecordList";
	}

	@Override
	protected String getEditView() {
		return "meal/mealRecordEdit";
	}

	@Override
	protected String getListMapper() {
		return "com.dingjian.meal.dao.MealRecordDao.select";
	}

	@Override
	protected BaseService<MealRecord> getService() {
		return mealRecordService;
	}
}
